package web;

import java.util.List;

import model.League;
import model.User;
import service.LeagueService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

import exceptions.DataException;
import exceptions.LeagueNotFoundException;
import exceptions.LeagueRestrictionException;
import exceptions.UserNotFoundException;

/**
 * Class for manipulates actions of the League in web application
 * @author Andre Melo
 * @author Marcus Leite 
 * @author Sergio Danilo
 * 
 */
public class LeagueAction extends BLMSAction implements Preparable {

	private static final long serialVersionUID = 8383793589804468375L;

	//Attributes
	private Integer id;
	
	private LeagueService leagueService;
	
	private League league;
	
	private List<League> list;
	
	private List<User> users;
	
	private List<User> members;
	
	private Integer operatorId;
	
	private Integer initialHandicap;
	
	private Integer userId;
	
	private Integer idLeagueRemoveMember;
	
	/**
	 * The main method for web application.
	 * @return Success or fail in the application.
	 */
	public String main() {
		return Action.SUCCESS;
	}
	
	/**
	 * The input method.
	 * @return INPUT.
	 */
	public String input() {
		return Action.INPUT;
	}
	
	/**
	 * Save the league application
	 * @return All leagues
	 */
	public String save() {
		try {
			if (this.league.getId() == null) {
				System.out.println("Saving new league");
				leagueService.save(league, operatorId);
			} else {
				System.out.println("Updating");
				leagueService.update(league);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Action.INPUT;
		}
		league = new League();
		return listAll();
	}
	
	/**
	 * Delete the league
	 * @return Sucess or fail
	 * @throws LeagueRestrictionException
	 */
	public String delete(){
		leagueService.delete(id);
		return listAll();
	}
	
	/**
	 * Add member in a league.
	 * @return SUCCESS if the addition occurs well, ERROR in other case.
	 */
	public String addMember(){
		try {
			leagueService.joinLeague(userId, id, initialHandicap);
			listMembers();
		} catch (DataException e) {
			return Action.ERROR;
		} catch (LeagueNotFoundException e) {
			return Action.ERROR;
		} catch (LeagueRestrictionException e) {
			return Action.ERROR;
		} catch (UserNotFoundException e) {
			return Action.ERROR;
		}
		return Action.SUCCESS;
	}
	
	/**
	 * Remove member from a league.
	 * @return The list of all league members if occurs well, ERROR in other case.
	 */
	public String removeMember(){
		try {
			leagueService.leaveLeague(userId, idLeagueRemoveMember);
			System.out.println("Removi o cabra");
		} catch (LeagueRestrictionException e) {
			return Action.ERROR;
		} catch (UserNotFoundException e) {
			return Action.ERROR;
		} catch (LeagueNotFoundException e) {
			return Action.ERROR;
		}
		return listMembers();
	}
	
	/**
	 * List all members of the league.
	 * @return SUCCESS if the addition occurs well, ERROR in other case.
	 */
	public String listMembers(){
		try {
			System.out.println("Member list");
			league = leagueService.findById(id);
			members = leagueService.getLeagueMembers(id);
			idLeagueRemoveMember = id;
		} catch (LeagueNotFoundException e) {
			e.printStackTrace();
			return Action.ERROR;
		}
		return Action.SUCCESS; 
	}
	
	/**
	 * List all existing leagues.
	 * @return All existing leagues.
	 */
	public String listAll() {
		list = leagueService.listAll();
		return Action.SUCCESS;
	}
	
	/**
	 * Get the ID of the league
	 * @return The ID of the league
	 */
	public Integer getId() {
		return id;
	}

	public String findById() {
		leagueService.findById(id);
		return Action.SUCCESS;
	}
	
	/**
	 * Set the ID of the league
	 * 
	 * @param id The ID of the league
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Set the attribute leagueService
	 * @param leagueService The attribute leagueService
	 */
	public LeagueService getLeagueService() {
		return leagueService;
	}

	/**
	 * Set the attribute leagueService
	 * @param leagueService The attribute leagueService
	 */
	public void setLeagueService(LeagueService leagueService) {
		this.leagueService = leagueService;
	}

	
	
	/**
	 * Get the current league
	 * @return The current league
	 */
	public League getLeague() {
		return league;
	}

	/**
	 * Set the current league
	 */
	public void setLeague(League league) {
		this.league = league;
	}

	/**
	 * Get the list of the leagues
	 * @return The list of the leagues
	 */
	public List<League> getList() {
		return list;
	}

	/**
	 * Set the list of the leagues
	 */
	public void setList(List<League> list) {
		this.list = list;
	}

	/**
	 * Get all users of the league
	 * @return All users of the league
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * Set some users of the league
	 * @param users Some users of the league
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * Get the operator of the league
	 * @return The operator of the league
	 */
	public Integer getOperatorId() {
		return operatorId;
	}

	/**
	 * Set the ID of the league's operator
	 * @param operatorId The new ID of the league's operator
	 */
	public void setOperatorId(Integer operatorId) {
		this.operatorId = operatorId;
	}

	@Override
	public void prepare() throws Exception {
		users = userService.listAll();
		list = leagueService.listAll();
		
	}

	/**
	 * Get the members of the league.
	 * @return
	 */
	public List<User> getMembers() {
		return members;
	}

	/**
	 * Set the members of the league.
	 * @param members The members of the league.
	 */
	public void setMembers(List<User> members) {
		this.members = members;
	}

	/**
	 * Get the initial handicap of a player.
	 * @return The initial handicap of a player.
	 */
	public Integer getInitialHandicap() {
		return initialHandicap;
	}

	/**
	 * Set the initial handicap of a player.
	 * @param initialHandicap The initial handicap of a player.
	 */
	public void setInitialHandicap(Integer initialHandicap) {
		this.initialHandicap = initialHandicap;
	}

	/**
	 * Get the ID of a player.
	 * @return The ID of a player.
	 */
	public Integer getUserId() {
		return userId;
	}

	/**
	 * Set the ID of a player.
	 * @param userId The ID of a player.
	 */
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	
}
